package com.panda.mall.user.tool.controller;

import com.panda.mall.common.base.EnumBean;
import com.panda.mall.common.base.EnumInterface;
import com.panda.mall.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.reflections.Reflections;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * author: tcy
 * createDate: 2023/1/31
 * description:公用工具方法接口
 */
@Api(tags = "公共方法")
@RestController
@RequestMapping("/user")
public class ToolController {

    @ApiOperation(value = "获取枚举", notes = "获取枚举")
    @GetMapping("/tool/getEnums")
    public Result<Map<String, List<EnumBean>>> getEnums(){

        Map<String, List<EnumBean>> enums = new HashMap<>();

        Reflections reflections = new Reflections("com.panda.mall.common");

        Set<Class<? extends EnumInterface>> monitorClasses = reflections.getSubTypesOf(EnumInterface.class);

        //System.out.println("size:"+monitorClasses.size());
        monitorClasses.forEach(m->{
            try {
                //System.out.println("name:"+m.getSimpleName());

                List<EnumBean> enumBeans = new LinkedList<>();
                EnumInterface[] enumConstants = m.getEnumConstants();

                for (EnumInterface anEnum : enumConstants) {
                    //System.out.println("code:"+anEnum.getCode()+"  value:"+anEnum.getValue()+"  key:"+anEnum.getKey());
                    EnumBean enumBean =new EnumBean();
                    enumBean.setCode(anEnum.getCode());
                    enumBean.setKey(anEnum.getKey());
                    enumBean.setValue(anEnum.getValue());
                    enumBeans.add(enumBean);
                }
                enums.put(m.getSimpleName(),enumBeans);

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return Result.success(enums);
    }

}
